﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Extract emails</title>
	<link href="styles/js-console.css" rel="stylesheet" />
	<script src="scripts/string-extensions.js"></script>
</head>
<body>							  

	<div id="js-console"></div>
	<script src="scripts/js-console.js"></script>
	<script>
	    var text = "Here are some emails vasil.petrov@abv.bg and may be some more mails petar@gmail.bg ";
	    jsConsole.writeLine(text);
	    var emailArr = [];
	    var index = text.indexOf("@");
	    while (index > -1) {
	        if (text[index - 1] != " " && text[index + 1] != " ") {
	            var identifier = text.substring(text.lastIndexOf(" ", index - 1), index);
	            var host = text.substring(index + 1, text.indexOf(".", index + 1));
	            var domain = text.substring(text.indexOf(".", index + 1) + 1, text.indexOf(" ", text.indexOf(".", index + 1) + 1));
	            var email = identifier + "@" + host + "." + domain;//it's not working slow for 5 elements
	            emailArr.push(email);
	            index = text.indexOf("@", index + 1);
	        }
	    }
	    for (var email in emailArr) {
	        jsConsole.writeLine(emailArr[email]);
	    }
	</script>

</body>
</html>